package com.mobvel.zxstdzgl.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;

import com.mobvel.zxstdzgl.R;
import com.mobvel.zxstdzgl.model.Book;
import com.mobvel.zxstdzgl.model.CateInfo;
import com.mobvel.zxstdzgl.model.Chapter;

public class DBHelper {
	private String DATABASE_PATH;
	private Context mContext;
	private SQLiteDatabase mDB;
	

	public DBHelper(Context context) {
		mContext = context;
		
		String str1 = Environment.getExternalStorageDirectory().getAbsolutePath();
		String str2 = str1 + File.separator +"ReaderCollections";
		DATABASE_PATH = str2;
		
		mDB = openDatabase();
	}

	public List<Book> getCate() {
		List<Book> localArrayList = new ArrayList<Book>();
		Cursor localCursor = mDB.rawQuery("select * from type ", null);
		while (true) {
			if (!localCursor.moveToNext()) {
				localCursor.close();
				return localArrayList;
			}
			Book localBook = new Book();
			int i = localCursor.getInt(0);
			localBook.setId(i);
			String str = localCursor.getString(1);
			localBook.setName(str);
			localArrayList.add(localBook);
		}
	}

	public List<CateInfo> getCateList() {
		ArrayList<CateInfo> cateList = new ArrayList<CateInfo>();
		Cursor localCursor = mDB.rawQuery("select * from type ", null);
		while (true) {
			if (!localCursor.moveToNext()) {
				localCursor.close();
				return cateList;
			}
			int cateId = localCursor.getInt(0);
			long num = getArticleNum(cateId);
			String cateName = localCursor.getString(1);
			CateInfo cateInfo = new CateInfo(cateId, cateName, num);
			
			cateList.add(cateInfo);
		}
	}
	
	private long getArticleNum(int cateId) {
		String str1 = "select count(*) from chapter where typeid = " + cateId;
		Cursor localCursor = mDB.rawQuery(str1, null);
		localCursor.moveToFirst();
		long num = localCursor.getLong(0);
		Log.e("xx", "num = "+num);
		
		localCursor.close();
		
		return num;
	}

	public List<Chapter> getChapterByBookId(int paramInt) {
		ArrayList<Chapter> localArrayList = new ArrayList<Chapter>();
		String str1 = "select * from chapter where typeid = " + paramInt;
		Cursor localCursor = mDB.rawQuery(str1, null);
		while (true) {
			if (!localCursor.moveToNext()) {
				localCursor.close();
				return localArrayList;
			}
			Chapter localChapter = new Chapter();
			localChapter.setBookId(paramInt);
			int i = localCursor.getInt(0);
			localChapter.setChapterId(i);
			String str2 = localCursor.getString(1);
			localChapter.setName(str2);
			localArrayList.add(localChapter);
		}
	}

	public SQLiteDatabase openDatabase() {
		try {
			String str = DATABASE_PATH + File.separator + "dbfile";
			File localFile = new File(DATABASE_PATH);
			if (!localFile.exists()) {
				localFile.mkdir();
			}
			
			if (new File(str).exists()) {
				return SQLiteDatabase.openOrCreateDatabase(str, null);
			}else {
				InputStream input = mContext.getResources().openRawResource(R.raw.dbfile);
				if(null == input) {
					return null;
				}
				byte[] buffer = new byte[8196];
				FileOutputStream output = new FileOutputStream(str);
				while(input.read(buffer) > 0) {
					output.write(buffer);
				}
				output.close();
				input.close();
				return SQLiteDatabase.openOrCreateDatabase(str, null);
			}
			
		} catch (Exception localException) {
			//
			return null;
		}
	}
	
	public void closeDB() {
		mDB.close();
	}
}